package com.swjd.controller;

import com.swjd.pojo.User;
import com.swjd.pojo.UserKey;
import com.swjd.service.impl.*;
import com.swjd.util.BecomeJson;
import com.swjd.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by hxl on 2019/5/31  9:11
 */
@Controller
@RequestMapping(value = "action/", method = RequestMethod.POST)
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ImageServiceImpl imageService;
    @Autowired
    private ConcernServiceImpl concernService;
    @Autowired
    private GoodsServiceImpl goodsService;
    @Autowired
    private OrdersServiceImpl ordersService;

    @ResponseBody
    @RequestMapping("login")//根据UserKey账号密码验证账号密码是否正确,并返回User对象
    public User loginUser(@RequestBody UserKey key) {
        key.setPassword(Utils.getMd5(key.getPassword()));
        User user = userService.selectUser(key);
        System.out.println("aaa"+key.getUsername());
        return selectUserInfo(user);
    }

    public User selectUserInfo(User user) {
        String username = user.getUsername();
        user.setImage(imageService.selectImgUrl(user.getUsername()));
        user.setFans(concernService.selectFans(username));
        user.setConcerns(concernService.selectConcern(username));
        user.setGoodsList(goodsService.selectUserByGoods(username));
        user.setOrdersList(ordersService.selectUserByOrders(username));
        return user;
    }

    @ResponseBody
    @RequestMapping("selectUserByPhone")//用户通过手机号登录,返回User对象
    public User selectUserByPhone(@RequestParam("phone") String tel) {
        User user = userService.selectUserByPhone(tel);
        return selectUserInfo(user);
    }

    @ResponseBody
    @RequestMapping("register")//通过User对象进行注册用户信息
    public String insertUser(@RequestBody User user) {
        user.setPassword(Utils.getMd5(user.getPassword()));
        String back = BecomeJson.isOk(userService.insertUser(user));
        return back;
    }

    @ResponseBody
    @RequestMapping("updateByUser")//修改用户信息
    public String updateByUser(@RequestBody User user) {
        return BecomeJson.isOk(userService.updateByUser(user));
    }


}
